﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Neat;
using Neat.Mathematics;
using Neat.GUI;
using Neat.Graphics;
using Sectors;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using SectorsDesigner.MapMaker;
#endregion

namespace SectorsDesigner
{
    public partial class TextureChooserControl : GraphicsDeviceControl
    {
        TextureChooserDialog Dialog;
        SectorsMapMakerControl Designer;
        public Vector2 ThumbnailSize = new Vector2(128);
        public Vector2 Offset = new Vector2(4);
        public Vector2 ItemsOffset = new Vector2(16);
        public int FirstIndex = 0;
        new public Point Size;
        public Vector2 MousePosition;
        Texture2D SelectTex;
        public Point GridSize
        {
            get
            {
                Vector2 box = new Vector2(Size.X, Size.Y) - 2 * Offset;
                Vector2 count = box / (ItemsOffset + ThumbnailSize);
                return new Point((int)count.X-1 , (int)count.Y-1);
            }
        }
        public int ThumbsCount
        {
            get
            {
                return GridSize.X * GridSize.Y;
            }
        }

        SpriteBatch spriteBatch;
        public TextureChooserControl(TextureChooserDialog dialog, SectorsMapMakerControl designer)
        {
            Dialog = dialog;
            Designer = designer;
        }

        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Color[] pixmap = new Color[(int)(ThumbnailSize.X * ThumbnailSize.Y)];
            for (int x = 0; x < ThumbnailSize.X; x++)
                pixmap[x] = pixmap[x + (int)((ThumbnailSize.Y-1) * ThumbnailSize.X)] = Color.White;
            for (int y = 0; y < ThumbnailSize.Y; y++)
                pixmap[(int)(y * ThumbnailSize.X)] = pixmap[(int)((ThumbnailSize.X - 1) + (ThumbnailSize.X * y))] = Color.White;
            SelectTex = new Texture2D(GraphicsDevice, (int)ThumbnailSize.X, (int)ThumbnailSize.Y);
            SelectTex.SetData(pixmap);
        }

        protected override void Draw()
        {
            var IsMouseClicked = Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed;

            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            var grid = GridSize;
            var n = ThumbsCount;
            Point o = Point.Zero;
            for (int i = FirstIndex; i < ThumbsCount && i < Dialog.Textures.Length; i++)
            {
                Rectangle r = new Rectangle((int)(Offset.X + o.X * (ThumbnailSize.X + ItemsOffset.X)), (int)(Offset.Y + o.Y * (ThumbnailSize.Y + ItemsOffset.Y)),
                    (int)ThumbnailSize.X, (int)ThumbnailSize.Y);
                
                spriteBatch.Draw(Dialog.Textures[i], r, Color.White);
                
                if ((IsMouseClicked && GeometryHelper.IsVectorInRectangle(r, MousePosition)) || Dialog.TextureNameBox.Text == Dialog.Keys[i])
                {
                    if (Dialog.TextureNameBox.Text != Dialog.Keys[i]) Dialog.TextureNameBox.Text = Dialog.Keys[i];
                    spriteBatch.Draw(SelectTex, r, Color.Red);
                }
                o.Y++;
                if (o.Y > grid.Y)
                {
                    o.Y = 0;
                    o.X++;
                }
            }

            spriteBatch.End();
        }
    }
}